package com.notary.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.notary.entity.Contract;
import com.notary.entity.ContractIssue;
import com.notary.entity.Deposit;
import com.notary.entity.Person;
import com.notary.entity.vo.JsonResult;
import com.notary.service.DepositService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

@Api(tags = "电子存证")
@RestController
@RequestMapping("/deposit")
public class DepositController {

    @Resource
    private DepositService depositService;

    @PostMapping("/queryList")
    public JsonResult<IPage<Deposit>> queryList(@RequestBody JSONObject jsonObject) {
        return depositService.queryList(jsonObject);
    }

    @ApiOperation(value = "存证校验")
    @PostMapping("/verifyDeposit/{id}")
    public JsonResult verifyDeposit(@PathVariable Integer id, @RequestParam(required = false) MultipartFile file, @RequestParam(required = false) String hashCode, @RequestParam(required = false) String originalHash) {

        if (Objects.isNull(originalHash)) {
            return JsonResult.fail("源文件哈希值不能为空！");
        }
        if (Objects.isNull(hashCode) && Objects.isNull(file)) {
            return JsonResult.fail("存证校验哈希值或者源文件不能为空！");
        }
        return depositService.checkDeposit(id, file, hashCode, originalHash);
    }

    @ApiOperation(value = "解压存证文件")
    @PostMapping("/unzip/{id}")
    public JsonResult unzip(@PathVariable Integer id,@RequestBody JSONObject jsonObject) {
        return depositService.unzip(id, jsonObject.getString("hashCode"));
    }

    @ApiOperation(value = "出证")
    @GetMapping("/chuzheng/{id}")
    public JsonResult chuzheng(@PathVariable Integer id) throws Exception {
        return depositService.chuzheng(id);
    }

    @PostMapping("/applyChuZheng")
    public JsonResult applyChuZheng(@RequestBody JSONObject jsonObject) {
        return depositService.applyChuZheng(jsonObject);
    }

    @PostMapping("/download")
    public JsonResult download(@RequestBody JSONObject jsonObject) {
        return depositService.download(jsonObject);
    }
}
